package com.sanyang.pda.ui.takestock;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.sanyang.pda.adapter.StockAdapter;
import com.sanyang.pda.bean.ScanBean;
import com.sanyang.pda.bean.TakeStockBean;
import com.sanyang.pda.bean.request.TaskStockCompleteRequest;
import com.sanyang.pda.contract.StockContract;
import com.sanyang.pda.presenter.StockPresenter;
import com.sanyang.pda.utils.StringConfig;
import com.sanyang.pda.utils.UserUtils;
import com.sanyang.pda.view.ScanButView;
import com.wangshen.base.app.AppActivityKey;
import com.wangshen.base.glide.GlideEngine;
import com.wangshen.base.ui.mvp.base.ui.BaseMvpFragment;
import com.wangshen.projectbase.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class StockFragment extends BaseMvpFragment<StockPresenter> implements StockContract.View {
    @BindView(R.id.tv_storagebinId)
    TextView tvStoragebinId;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.sb_scan)
    ScanButView sbScan;
    @BindView(R.id.bt_wancheng)
    Button btWancheng;
    @BindView(R.id.bt_add)
    Button btAdd;
    @BindView(R.id.bt_clear)
    Button btClear;
    @BindView(R.id.bt_photograph)
    Button btPhotograph;
    @BindView(R.id.iv_pic1)
    ImageView ivPic1;
    @BindView(R.id.iv_close1)
    ImageView ivClose1;
    @BindView(R.id.ll_pic1)
    FrameLayout llPic1;
    @BindView(R.id.iv_pic2)
    ImageView ivPic2;
    @BindView(R.id.iv_close2)
    ImageView ivClose2;
    @BindView(R.id.ll_pic2)
    FrameLayout llPic2;
    @BindView(R.id.iv_pic3)
    ImageView ivPic3;
    @BindView(R.id.iv_close3)
    ImageView ivClose3;
    @BindView(R.id.ll_pic3)
    FrameLayout llPic3;

    private String image1, image2, image3;
    private String entryNo;

    public StockFragment(String entryNo) {
        this.entryNo = entryNo;
    }

    public StockFragment() {

    }

    private StockAdapter stockAdapter;

    @Override
    public StockPresenter getPresenter() {
        return new StockPresenter();
    }

    @Override
    public int getViewId() {
        return R.layout.fragment_stock;
    }

    @Override
    public void initView() {
        stockAdapter = new StockAdapter(R.layout.item_stock_take_list, new ArrayList<>());
        rvList.setLayoutManager(new LinearLayoutManager(mContext));
        rvList.setAdapter(stockAdapter);
    }

    @Override
    public void initLinsenterner() {
        sbScan.setScanCallBack(new ScanButView.ScanCallBack() {
            @Override
            public void callBack(ScanBean scanBean) {
                if (scanBean.getType() == 1) {
                    tvStoragebinId.setText(scanBean.getCode());
                    presenter.stockList(scanBean.getCode(),entryNo);
                }
            }
        });
        stockAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                Bundle bundle = new Bundle();
                stockAdapter.getData().get(position).setTaskNo(entryNo);
                bundle.putSerializable(StringConfig.BEAN, stockAdapter.getData().get(position));
                startIntent(AppActivityKey.CONFIRMSTOCKACTIVITY, bundle);
            }
        });
    }

    @Override
    public void getData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        if (!TextUtils.isEmpty(tvStoragebinId.getText())) {
            presenter.stockList(tvStoragebinId.getText().toString(),entryNo);
        }
    }

    @OnClick(R.id.bt_wancheng)
    public void onBtWanchengClicked() {
        if (TextUtils.isEmpty(tvStoragebinId.getText().toString())) {
            showMessage(getString(R.string.storagebinId) + getString(R.string.not_be_empty));
            return;
        }
        TaskStockCompleteRequest taskStockCompleteRequest = new TaskStockCompleteRequest();
        taskStockCompleteRequest.setStoragebinId(tvStoragebinId.getText().toString());
        taskStockCompleteRequest.setTaskNo(entryNo);
        taskStockCompleteRequest.setPic1Address(image1);
        taskStockCompleteRequest.setPic2Address(image2);
        taskStockCompleteRequest.setPic3Address(image3);
        taskStockCompleteRequest.setCountUser(UserUtils.getInstance().getUserBean().getUserName());
        presenter.getTaskStockComplete(taskStockCompleteRequest);
    }

    @OnClick(R.id.bt_add)
    public void onBtAddClicked() {
        if (TextUtils.isEmpty(tvStoragebinId.getText().toString())) {
            showMessage(getString(R.string.storagebinId) + getString(R.string.not_be_empty));
            return;
        }
        Bundle bundle = new Bundle();
        bundle.putString(StringConfig.STORAGEBINID, tvStoragebinId.getText().toString());
        bundle.putString(StringConfig.TASKNO, entryNo);
        startIntent(AppActivityKey.ADDSTOCKACTIVITY, bundle);
    }

    @Override
    public void setList(List<TakeStockBean> list) {
        stockAdapter.setNewInstance(list);
    }

    @Override
    public void onStockSucces() {
        tvStoragebinId.setText("");
        stockAdapter.setNewInstance(new ArrayList<>());
    }

    @Override
    public void setImageUrl(String url) {
        if (TextUtils.isEmpty(image1)){
            image1=url;
            llPic1.setVisibility(View.VISIBLE);
            Glide.with(mContext).load(url).into(ivPic1);
            return;
        }
        if (TextUtils.isEmpty(image2)){
            image2=url;
            llPic2.setVisibility(View.VISIBLE);
            Glide.with(mContext).load(url).into(ivPic2);
            return;
        }
        if (TextUtils.isEmpty(image3)){
            image3=url;
            llPic3.setVisibility(View.VISIBLE);
            Glide.with(mContext).load(url).into(ivPic3);
            return;
        }
    }

    @OnClick(R.id.bt_clear)
    public void onViewClicked() {
        tvStoragebinId.setText("");
        stockAdapter.setNewInstance(new ArrayList<>());
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        List<LocalMedia> images;
        if (resultCode == -1) {
            if (requestCode == PictureConfig.CHOOSE_REQUEST) {// 图片选择结果回调
                images = PictureSelector.obtainMultipleResult(data);
                presenter.uploadImage(images.get(0).getCompressPath());
            }
        }
    }

    @OnClick(R.id.bt_photograph)
    public void onViewClicked1() {
        if (!TextUtils.isEmpty(image1) && !TextUtils.isEmpty(image2) && !TextUtils.isEmpty(image3)){
            showMessage(getString(R.string.zuiduosanzhang));
            return;
        }
        PictureSelector.create(mContext)
                .openGallery(PictureMimeType.ofImage())
                .imageEngine(GlideEngine.createGlideEngine())
                .maxSelectNum(1)
                .isCompress(true)
                .compressQuality(30)
                .synOrAsy(true)
                .minSelectNum(1)
                .imageSpanCount(4)
                .selectionMode(PictureConfig.MULTIPLE)
                .forResult(PictureConfig.CHOOSE_REQUEST);
    }

    @OnClick({R.id.iv_close1, R.id.iv_close2, R.id.iv_close3})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_close1:
                llPic1.setVisibility(View.GONE);
                image1="";
                break;
            case R.id.iv_close2:
                llPic2.setVisibility(View.GONE);
                image2="";
                break;
            case R.id.iv_close3:
                llPic3.setVisibility(View.GONE);
                image3="";
                break;
        }
    }
}
